package coop.bancocredicoop.proyectos.gd.crecerxxi;

import java.util.LinkedList;
import java.util.List;

import coop.bancocredicoop.crecerxxi.server.XStreamSerializer;
import coop.bancocredicoop.crecerxxi.server.exposicion.ResponseTO;
import coop.bancocredicoop.proyectos.gd.domain.Customer;
import coop.bancocredicoop.proyectos.gd.query.customer.FindCustomerByIdPersona;
import coop.bancocredicoop.proyectos.gd.query.customer.InsertCustomer;
import coop.bancocredicoop.service.core.response.OutputMessage;
import coop.bancocredicoop.service.core.sender.backport.socket.BackportMessageSender;
import coop.bancocredicoop.service.load.ServiceProvider;
import coop.bancocredicoop.service.transaccion.persona.factory.T24MessageSpecFactory;
import coop.bancocredicoop.service.transaccion.persona.parser.spec.ListaCandidatosMessageSpec;
import coop.bancocredicoop.service.transaccion.persona.specParser.ListaCandidatosTextBeanImpl;

public class CrecerXXIRealWrapper implements CrecerXXIWrapper {

	public List<CrecerXXICustomer> findCustomerByIdPersona(String tipoPersona,
			Long idPersona) throws Exception {

		ResponseTO response = null;

		ListaCandidatosMessageSpec spec = (ListaCandidatosMessageSpec)
				new T24MessageSpecFactory().getListasCandidatos();
		
		spec.withTipoPersona(tipoPersona);
		spec.withIdPersona(new coop.bancocredicoop.common.domain.numeric.Numeric(idPersona));

		spec.setParseable(false);

		BackportMessageSender service = (BackportMessageSender) ServiceProvider
				.getInstance().lookupService(BackportMessageSender.class);

		OutputMessage output = service.send(spec);

		response = (ResponseTO) XStreamSerializer.getInstance().deserialize(
				(String) output.getData());

		return getListaCandidatos(response);

	}

	private List<CrecerXXICustomer> getListaCandidatos(ResponseTO response) throws Exception {
		List<CrecerXXICustomer> listCrecerXXICustomer = null;
		
		if (response.hasError()) {
			throw new Exception(response.getMessage());
		}
		
		if (response.getData() != null) {
			List<ListaCandidatosTextBeanImpl> candidatos = (List<ListaCandidatosTextBeanImpl>) response.dataFromXML();

			listCrecerXXICustomer = new LinkedList<CrecerXXICustomer>();
		   
			for (ListaCandidatosTextBeanImpl candidato : candidatos) {
				listCrecerXXICustomer.add(new CrecerXXICustomer(candidato.getIdPersona().longValue(), candidato.getApellido(),
					   candidato.getNroInscripcion()));
			}		   
		}
		
		return listCrecerXXICustomer;

	}

	public List<CrecerXXICustomer> findCustomerByCuit(String tipoPersona,
			String cuit) throws Exception {

		ResponseTO response = null;

		ListaCandidatosMessageSpec spec = (ListaCandidatosMessageSpec)
				new T24MessageSpecFactory().getListasCandidatos();
		
		spec.withTipoPersona(tipoPersona);
		spec.withNroInscripcion(cuit);

		spec.setParseable(false);

		BackportMessageSender service = (BackportMessageSender) ServiceProvider
				.getInstance().lookupService(BackportMessageSender.class);

		OutputMessage output = service.send(spec);

		response = (ResponseTO) XStreamSerializer.getInstance().deserialize(
				(String) output.getData());

	   return getListaCandidatos(response);

	}

	public List<CrecerXXICustomer> findCustomerByNombre(String tipoPersona,
			String nombre) throws Exception {

		ResponseTO response = null;

		ListaCandidatosMessageSpec spec = (ListaCandidatosMessageSpec)
				new T24MessageSpecFactory().getListasCandidatos();
		
		spec.withTipoPersona(tipoPersona);
		spec.withApellido(nombre);

		spec.setParseable(false);

		BackportMessageSender service = (BackportMessageSender) ServiceProvider
				.getInstance().lookupService(BackportMessageSender.class);

		OutputMessage output = service.send(spec);

		response = (ResponseTO) XStreamSerializer.getInstance().deserialize(
				(String) output.getData());

		return getListaCandidatos(response);

	}

	public Customer findOrCreateCustomer(Long idPersona) throws Exception {

		Customer customer = new FindCustomerByIdPersona().run(idPersona);

		if (customer == null) {
			CrecerXXICustomer customerProperties = this.
					findCustomerByIdPersona("A", idPersona).get(0);

			Customer newCustomer = new Customer(null,
					0L,
					customerProperties.getInternalId(),
					customerProperties.getCuit(),
					customerProperties.getName());

			new InsertCustomer().run(newCustomer);

			return newCustomer;
        }

        return customer;

	}

}
